
//获取数据库驱动
const mongoose = require("mongoose");
const { productListModel } = require("../db/model/productlist.js")
const get = async ({ curPage, eachPage }) => {
    (curPage = ~~curPage), (eachPage = ~~eachPage);
    const data = {
        total: 0,
        maxPage: 0,
        rows: [],
    };
    //获取当前model映射的集合中的文档总数
    data.total = await productListModel.countDocuments();
    //获取总页数
    data.maxPage = Math.ceil(data.total / eachPage);
    const skipNum = (curPage - 1) * eachPage;
    // 获取页面渲染数据
    data.rows = await productListModel.find().skip(skipNum).populate("navsId").populate("tabsId").limit(eachPage).sort({ _id: -1 }).exec();
    console.log(data.rows);
    return data

};

// query(查询条件),params(修改的属性)
const updata = async (query, params) => {
    const productListModel = mongoose.model("productList");
    return await productListModel.updateOne(query, params)
}

// 搜索
const searchProduct = async (value) => {
    const productListModel = mongoose.model("productList");
    return await productListModel.find({
        $or: [{ name: eval(`/${value}/`)  }]
    }).populate("navsId").populate("tabsId")
}
// 新增商品接口
const add = async ({ navsId, tabsId, name, count, price, desc }) => {
    const productListModel = mongoose.model("productList");
    return await productListModel.create({ navsId, tabsId, name, count, price, desc })
}

const findOne = async id => {
    // model传一个参数时表示获取这个model对象
    const _id = id
    const productListModel = mongoose.model("productList");
    // cons
    await productListModel.findOne({ _id });
}

// 通过id获取数据
const getOne = async id => {
    const _id = id
    const productListModel = mongoose.model("productList");
    return await productListModel.findOne({ _id }).populate("navsId").populate("tabsId")
}
const delPoster = async ({ _id, url }) => {
    const productListModel = mongoose.model("productList");
    // findOne找到匹配的数据立即返回，不会接着匹配数据
    const value = await productListModel.findOne({ _id });
    // 删除url对应的图片，并保存！！！！！sava
    value.poster.pull(url);
    await value.save();
}

// 修改
// const changeproductList = async (newData) => {
//     const productListModel = mongoose.model("productList");
//     await productListModel.update({ _id: newData._id }, newData);
// }


// 获取所有电影 排片用的
const productListListAll = async () => await mongoose.model("productList").find();

// 删除电影信息
const remove = async ({ _id }) => {
    const productListModel = mongoose.model("productList");
    // cons
    await productListModel.remove({ _id });
}
// 删除图片
const delImg = async ({ _id, url }) => {
    const productListModel = mongoose.model("productList");
    // findOne找到匹配的数据立即返回，不会接着匹配数据
    const movie = await productListModel.findOne({ _id });
    // 删除url对应的图片，并保存！！！！！sava
    movie.images.pull(url);
    await movie.save();
}

// 修改
const changeProductList = async (newData) => {
    console.log(newData);
    const productListModel = mongoose.model("productList");
    await productListModel.update({ _id: newData._id }, newData);
}
module.exports = { get, add, getOne, updata, delImg, searchProduct, remove, findOne, delPoster, changeProductList, productListListAll };
